Okay, so in today's lesson we are going to again continue on with functions.
Okay.
And this will probably be the second to last lesson on functions.
Okay.
And today we're looking at events.
Now events are so useful in Roadblock Studio.
Okay.
And this is when functions really become the most useful.
Okay.
At least I think so.
So add a part and a script into the game and just reference that part, um, using your standard variable.
And I'm going to say it's got the parent this time and put it inside of it.
Be a bit more clear.
Okay.
So what actually is an event and how are they linked to functions.
Well, an event is a signal which can be fired when something occurs.
And events are not ones you set up and create yourself.
They've already been made by the studio.
All you need to do is just reference them so that when they are fired, it does something.
Okay, an example of an event is when one part collides with another part, okay.
That would fire an event that's called the touched event or the touched event.
Okay.
Another event is when a part has one of its properties changed that will fire the changed or changed
property.
Not parameter.
Sorry.
The the changed event.
Okay.
So when these events fire they produce some kind of signal which you can basically connect with a function.
So what happens is when the event fires so does the function.
And so the event basically causes the function to fire.
And then you can write whatever you like in that function, which basically means at the end result
you can have some code work you have, you can have some code run when a part or any, any kind of object
has one of its properties changed, or if it collides with another object or any other event that's
available to use as well.
So really useful.
And if you're looking to make an Obie as well, touch is very useful event for that one as well.
So you'll learn about some very useful stuff today.
Okay.
So.
The first thing we need to do is get the events.
Okay, so the events exist inside of the object.
So to get the parts events we would write part dot.
And if we scroll down anything with a little lightning bolt next to it is an event.
So here we have touched and changed forever to we're going to look at today.
And we also have touch ended, child added destroying child removed and can keep scrolling down.
You can keep seeing a couple of them.
There are quite a lot just like how with Roblox is owned functions.
There's quite a few events, so it's something which I'm going to introduce you today, but you're going
to have to go off and try out some of these things, because I simply cannot explain what each event
does.
It would just take way too long.
Okay.
And some of them are more useful than others.
So I covered the most useful ones.
But it's still a very good idea for you to try out some some yourself as well.
Okay, so let's reference the touched event.
So we're going to say part dot touched like that there okay.
And that is our event that we've referenced.
From here, we need to somehow connect it with a function, and to connect it we use one of Roblox's
own functions called connect.
Okay, so we write a colon and just write connect.
Okay.
And this in this in this function here we can put inside um any kind of function.
Okay.
Um but we don't have a function or ready to use.
So we're going to create one.
So to create a function we just write the keyword function again with a pair of brackets and press enter.
Okay.
Quite a lot going on there.
Okay I'll just go over it again.
So part is the object that we're wanting to do this to.
Okay.
Touched here is the event which you're wanting to reference.
So this bit here is referencing the event.
Okay.
We then need to connect that event to a function so that when the event fires a function runs.
So we can connect them using the colon connect um default function by Roblox.
Okay.
And inside the brackets for the connect function we want to create our own function.
Okay, so quite a lot of functions and stuff involved there, but um, hopefully that makes sense.
So any anything inside here will basically run when the part collides with another part.
So let's print collision and see what happens when we actually go ahead and play the game.
Okay.
This part here can't be anchored, otherwise it won't move.
And let's move it up to start off with.
Okay, so let's play the game and see what happens.
See any times it prints collision.
Okay, so it's already printed at six times because it's bounced up and down.
And if we start to walk over it we can see it prints collision quite a few times.
This reason, this reason for this is because we my legs here, because of the animation of my legs.
They touch it in multiple points.
Okay, which is why it does it quite a lot.
So, um, not only is it printing collision more than it should, more than you probably want it to
each time, but also it works for any object.
As we saw at the start, it collided with a base plate, not just players.
So don't get confused.
This will work if any other object collides with this object.
Okay, so usually you don't want that in games, okay.
The most common use for touched is for things like opening up a shop, for example, when you touch
a certain circle.
So we're going to have a look how to kind of properly use this touch to make it in its most useful form.
So we kind of have two things we need to fix.
The first thing we need to fix is to stop it printing in collision.
So to stop it from running the code multiple times to only to do it once per group of collisions, basically.
Okay, the second thing we need to fix is making it so it only works if the if the character collides
with it, not any other part.
Okay.
So let's look at the first issue first okay.
Um, and to fix that we use something called a bounce.
So make a variable called bounce and set this equal to false.
And in cyber touched we are going to make an if loop.
So an if an if statement which basically checks if the bounce is equal to false.
So if not the bounce which basically means if the bounce does equal if the bounce equals false, then
what we're going to do is we're going to then immediately set the bounce to equal true okay.
So what will happen is the bounce to starts off as false.
Then when it touches a part the first time, it makes sure the bounce is not false.
And then it turns it to true.
Okay.
Um, now bounce is true.
It's not able to pass this if statement again, because it won't work because the condition is not met.
The bounce is true, not false.
So under line eight is basically not allowed to do this event.
Do this event again.
Basically it can run it, but it won't pass through the if statements.
Then we write whatever code we want.
So here goes your code okay.
Then you will wait a couple of seconds, maybe one second okay.
Then set bounce back to false.
So what is happening here is, um, is not allowed to run any code unless this condition here is met.
The condition being that de balance is false.
Okay, so when we run this the first time it runs it will be false by default.
So this will work fine.
Then it immediately changes the bounce to true.
So until it gets to this line here, our if statement will not be able to work again because the condition
has not been met.
So basically it won't be able to repeat the code until it's um, run all the code needed and waited
an extra second, which stops the multiple collisions at once.
Because, um, it can only collide.
It can only, um, run the code once per second.
Okay, obviously the a bit of time for the code running.
So realistically it's like 1.000001 seconds, but you get the point.
It's roughly about one second in between each collision that you can possibly do.
Okay.
So if we just try this out by we have to print something print a just so we can see it working okay.
It's hopefully point eight once.
There we go.
And we go over it.
It prints a again, but only once each time you can see.
And if we try to do it quite a lot frequently it might not pick up each one, but we can see it only
does it once per second.
Okay.
So that fixes our first issue.
The second issue we want to fix is making sure that the it only works if a player collided with it.
Okay.
Um, now to do this not a player.
So a character is we're looking at a player exists in game dot.
Player's character exists in the workspace.
Okay.
In the lecture where I introduced to you local scripts, a lot of the concepts here are going to become
quite useful because we're talking about players and characters.
So what can we do to check to see if whatever collided with it was a was a body part of the player,
or of a body part from the character?
Well, there is a handy parameter which has already been set up in here called other part.
And this parameter here will store the parts which collided with my part.
Okay.
Um, other parts is the technical name for it, but it's usually called hit.
You might have seen that before.
So we're going to use hit for this.
So this is just a variable.
Um so it's a placeholder variable.
So it's it's just like a parameter we looked at last lesson.
Okay.
But this parameter will be given the value of whatever part collided with my part.
Okay.
So so let's I'm just going to play the game to explain to you how this could work.
So I want to check.
So this is my character here.
And I've got all my body parts here.
So I only want it to work if the character's body parts have done the collision basically.
Okay.
Um, so we need to figure out what do these parts have which no other parts have.
So what what's different with all of these parts with this part?
So basically what makes these parts, um, children of a character rather than children of something
else?
Okay, so have a think.
How can we tell apart characters from just normal parts?
Pause the lesson if you need to, and have a think what it might be.
Okay.
Um, so the answer is the humanoid.
Now, I probably touched on this very briefly in the introduction to local scripts lesson, but basically
only characters will contain a humanoid.
So what we can do is we can check if the if the parent of the hit part.
Has a humanoid inside of it, because the parent of all those parts was my character's model.
And inside that model there's a humanoid.
So I'm basically checking if its parent.
So basically, if its siblings to a humanoid, okay, if it's not siblings to a humanoid, then there's
no humanoid presence, so it can't be a character.
But if it is, then it has to be a character or an NPC.
It would also work for non-player characters as well.
Okay, if you only wanted it to work for players and not NPCs, you'd have to do a bit more coding in
there.
But we're just looking at it for characters in this case.
Okay, so how would we check that?
Well, um, we would.
So we need to basically reference this or check to see if there's a humanoid.
Okay.
Again, maybe pause for a lesson.
What can we use to check if something exists to check if an object exists?
It is a it's one of the functions we looked at.
Okay.
It's defined first child function.
Okay.
See how all this function stuff's coming together.
Um, fine.
First child will attempt to find a child with a given name.
Okay, if it's not fair, it won't throw an error.
A nasty key bit.
Because if this frozen error, um, is going to error every time a part which is not from a character
collides with our part, which is not very good.
Okay.
Um, so let's check that.
So in an if statement we can say if hit dot parent okay.
So that would then reference the part that collided with my part.
Its parent okay.
Its siblings to be humanoid in my character.
So basically then want to check to see if if the parent has a humanoid inside of it.
So colon find first child with a string as a name humanoid.
So this is the parts which collide with my parts.
It's parent finding a child with a name.
Humanoid.
Okay.
And if that if that is the case.
So if it does find a humanoid then it must mean that it's a, it's a, it's a character.
So it's a character which is, which is colliding with the part, not anything else.
Okay.
So what I'm going to do is I'm going to put this here inside of the debounce.
So actually, I could say if not debounce and hit dot parent oh hit dot parent colon find first child
humanoid.
So so it has to have no debounce.
So it has to have waited enough time and whatever collided with it must have a humanoid as a sibling.
Okay.
And if that is the case, we've we've kind of fixed both the issues.
So it's completely fine to write whatever code we'd like.
Because if you're trying to write code, which I don't know, let's say opens up a, a, a shop, okay.
If you try to open up a shop for a part, it's going to make no sense and it's going to throw a bunch
of errors.
Okay.
So that's why we need to make sure that it's a character.
So let's try this out.
So if we print um a.
So does it.
Print a as soon as I join the game so it can't need a part up in the air and the part has dropped,
but it's not printed a.
Okay, so even though it's collided with the base plate, it is not printed A because the base plate
is not siblings.
To a humanoid.
We go over the parts and it prints a so it can tell if I am a character or I'm a player simply because
I am siblings, or my body parts are siblings to a humanoid.
Okay.
Quite a lot there.
Okay.
Um, again, these are not easy at all, but there's lots of stuff you can do with the touch events,
so, um, might take a bit of practicing.
Okay, but that's fine because it's really useful, and it'd be really worth it.
Okay, now this is the main one, and this one has a lot more coding involved to make it useful.
But the dot dot change I'm just going to talk about as well okay.
So part dot changed.
Change is another event.
So we do it the same way we say colon connect to connect it with a new function which again has its
own parameters.
But this time it stores a parameter called property.
And this will store a string which is the name of the property that was changed.
So this function, this event here will fire when a, um when one of its properties have been changed.
So we'd probably want to check what property is.
So we could just say if property is equal to and then say transparency for example.
And if a transparency has been changed, then we could just let let us know.
Um, transparency has been changed okay.
And it will let us know if it changes.
So let's just try this out.
Select the part.
And let's change this bouncing.
Oh.
Okay.
It didn't work.
Why might that be?
Hmm.
That's strange.
Does spell it right.
Um.
See.
I believe I changed it.
I did it right.
Um, let me try and, um, just put in notes the this bit here in case that's having issues with it.
Won the game.
Might see one.
There we go.
It works now for some reason.
Um, maybe it was something in the Abu.
I think maybe the debounce could be affecting it, but.
Yeah.
So that's how the change works.
There's not as much code involved in order to make it useful, but I'd thought I'd just mention how
to do it anyway.
Okay, obviously that's not gonna be very useful.
Just printing.
But transparency has been changed.
The actual use for that.
Okay.
Um, maybe if you have a value which, um, controls, which basically lets you know if the player owns
a game pass.
Okay.
If that's changed and they suddenly do own this game pass, then you want to unlock certain areas or
give them special weapons and all this kind of stuff.
Okay.
So these are very useful.
Both of them.
Both of those two are pretty the most common ones you're going to use but don't touch especially is
probably the most useful.
Okay.
And in the coming lessons, we are going to look at some examples of these and how we can kind of apply
them to some other objects which are probably find out that even more useful than you think at the moment.
Okay.
So they're probably quite difficult.
So make sure you have a practice of those.
Okay.
Um, as are very important.
So make sure you practice those and then join me in the next lesson where we're going to finish off
functions by looking at what returning is and how to do it.